import { Application } from "../App.ts";
import { STActor } from "../Core/STActor.ts";
import { AdvanceSTActor } from "../types/index.ts";

export class ReportScene {
    // 🙂 用于存放生成后处理报告的 渲染对象
    reportScene: STActor [] = []
    parent: Application
    reportScreenShotBase64: string [] = []

    constructor(parent: Application) {
        this.parent = parent
    }

    clear() {
        this.reportScene.forEach((item) => {
            this.parent.remove(item)
        })
        this.reportScene = []
    }

    /**
     * 隐藏所有场景对象
     * @param render - 是否立即渲染
     */
    hideAll(render = false) {
        this.reportScene.forEach((actor) => {
            actor.visible = false;
            (actor as AdvanceSTActor).hideLabels()
            this.autoHandleScalarBar(actor)
        })
        if (render) {
            this.parent.renderAtOnce()
        }
    }

    show(index: number) {
        if(index >= this.reportScene.length) return
        this.reportScene[index] && (this.reportScene[index].visible = true)
        this.autoHandleScalarBar(this.reportScene[index]);
        (this.reportScene[index] as AdvanceSTActor).showLabels();
        this.parent.renderer.resetCamera()
        this.parent.renderAtOnce()
    }

    /**
     * 隐藏其余只显示对应索引的对象
     * @param index
     */
    showOnly(index: number) {
        this.hideAll()
        this.show(index)
    }

    add(ac: STActor) {
        this.reportScene.push(ac)
        this.parent.add(ac)
        // * 处理标签文字
        if((ac as AdvanceSTActor).labelBox){
            this.parent.container.appendChild((ac as AdvanceSTActor).labelBox)
        }
    }

    private createReportSceneScreenShot(index: number): Promise<string> {
        this.hideAll()
        this.show(index)
        this.parent.renderer.resetCamera()
        this.parent.renderAtOnce()
        return new Promise((resolve) => {
            // ➡️ 异步捕获
            setTimeout(() => {
                this.parent.window.captureImages()[0].then(s => {
                    this.reportScreenShotBase64[index] = s
                    resolve(s)
                })
            }, 100)
        })
    }

    /**
     * 获取报告场景中某个对象的截图
     * @param index
     */
    getReportScene(index: number): Promise<string> {
        if (this.reportScreenShotBase64[index]) {
            return Promise.resolve(this.reportScreenShotBase64[index])
        } else {
            return this.createReportSceneScreenShot(index)
        }
    }

    /**
     * 递归完成所有对象的截图
     */
    createAllScreenShot() {
        return new Promise((resolve) => {
            let flag = 0;
            const next = (i: number) => {
                this.getReportScene(i).then(() => {
                    if (i < this.reportScene.length - 1) {
                        next(i + 1)
                    } else {
                        resolve(flag)
                    }
                })
            }
            next(flag)
        })
    }

    private autoHandleScalarBar(ac: STActor) {
        if (!ac || ac.components.scalarBar.instance === null) return
        if (ac.visible && ac.components.scalarBar.isShow) {
            this.parent.renderer.addActor(ac.components.scalarBar.instance)
        } else {
            this.parent.renderer.removeActor(ac.components.scalarBar.instance)
        }
    }
}